<?php
/**
 * Created by PhpStorm.
 * Author: wenwenbin
 * Date: 2018/3/3
 * Time: 13:21
 */
namespace App\Http\Admin\Personnel\Controllers;

use App\Http\Admin\AppManage\Models\UserRole;
use App\Http\Admin\Personnel\Models\AddressList;
use Framework\BaseClass\Http\Admin\Controller;
use App\Http\Admin\Personnel\Models\PassFlow;

class PassFlowController extends Controller
{
    public function passFlowList()
    {
        $company_id = session('current_operation_company_id');
        if (!$company_id) xThrow(ERR_CURRENT_OPERATION_COMPANY_ID_NOT_EXIST);
        try {
            if (request()->isMethod('post')) {
                $paging = $this->getPagingRequestParameters();
                $employee = new PassFlow();
                return $employee->getPassFlowList($company_id, $paging);
            }
        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }
        return $this->view('pass-flow-list');
    }

    public function joinCompany()
    {
        $params = request(['id', 'role_id', 'employee_number', 'prefix', 'suffix', 'length']);
        $company_id = session('current_operation_company_id');
        if (!$company_id) xThrow(ERR_CURRENT_OPERATION_COMPANY_ID_NOT_EXIST);
        try {
            $employee = new PassFlow();
            if (request()->isMethod('post')) {
                $idArray = explode(',', $params['id']);
                if (!$params['employee_number']) {
                    if ($params['length'] - mb_strlen($params['suffix']) < 0) return $this->ajaxFail('后缀长度大于设定的长度');
                    $result = $employee->joinCompany($idArray, $params['role_id'], null, $company_id,
                        $params['prefix'], $params['suffix'], $params['length']);
                } else {
                    $result = $employee->joinCompany($idArray, $params['role_id'], $params['employee_number'], $company_id);
                }
                return $result ? $this->ajaxSuccess() : $this->ajaxFail();
            }
            $company_id = session('current_operation_company_id');
            if (!$company_id) xThrow(ERR_CURRENT_OPERATION_COMPANY_ID_NOT_EXIST);
            $ids = '';
            if (is_array($params['id'])) {
                $count = count($params['id']);
                foreach ($params['id'] as $k => $value) {
                    if ($k != 0) $ids .= ',';
                    $ids .= $value;
                }
                $employeeNumberList = [];
                $batch = true;
            } else {
                $ids = $params['id'];
                $employee = new AddressList();
                $employeeNumberList = $employee->getEmployeeNumberList($company_id);
                $batch = false;
                $count = 0;
            }
            $userList = new UserRole();
            $roleList = $userList->roleListByCompanyId($company_id);
            return $this->view('join-company', [
                'id'                 => $ids,
                'count'              => $count,
                'batch'              => $batch,
                'employeeNumberList' => $employeeNumberList,
                'roleList'           => $roleList
            ]);
        } catch (\DebugError $e) {
            return $this->ajaxFail($e->getMessage());
        }
    }
}